package org.wyf.EveryPoint.crowd.service.impl;

import org.wyf.EveryPoint.common.entity.QueryRequest;

import org.wyf.EveryPoint.crowd.entity.Comment;
import org.wyf.EveryPoint.crowd.mapper.CommentMapper;
import org.wyf.EveryPoint.crowd.service.ICommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * 留言表 Service实现
 *
 * @author wyf
 * @date 2021-04-26 23:08:41
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    private final CommentMapper commentMapper;

    @Override
    public IPage<Comment> findComments(QueryRequest request, Comment comment) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<Comment> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Comment> findComments(Comment comment) {
	    LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createComment(Comment comment) {
        this.save(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateComment(Comment comment) {
        this.saveOrUpdate(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteComment(Comment comment) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    /**
     * 查询该项目的所有留言
     * @param projectId
     * @return
     */
    @Override
    public List<Comment> listCommentsByPid(Long projectId) {
        return this.commentMapper.listCommentsByPid(projectId);
    }
}
